<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 文章ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/content/contentArticle.class.php');

class ajax_articles {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询文章信息
                    $this->searchArticleList();
                    break;

                case 100:
                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit','change'))) {

                        returnJsonData('参数错误');

                    }

                    if ($_REQUEST['act'] == 'edit') {

                        $this->saveArticle();//添加或修改 文章

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteArticle();//删除文章

                    } else if ($_REQUEST['act'] == 'change') {

                        $this->changeArticleUseflg(); //文章 启用/禁用

                    }

                    break;

                default:
                    returnJsonData('参数错误');
                    break;


            }

        } else {
            returnJsonData('参数错误');
        }

    }

    /**
     * 查询文章信息
     */
    private function searchArticleList() {

        global $G_PGLOB;

        //实例文章类
        $contentArticle = new contentArticle();

        //查询文章信息
        $article_array = $contentArticle->searchArticle( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($article_array) {

            $edit_permission = $G_PGLOB->has_permission(1575);

            foreach($article_array as &$article) {

                if($edit_permission){

                    $article_id = $article['article_id'];

                    $preview_url = '/shop/article.php?subsite=shop&aid=' . $article_id . '&' . echo_preview_url();
                    $publish_url = '/shop/article.php?subsite=shop&aid=' . $article_id . '&' . echo_publish_url();

                    $operation = "";
                    if(!empty($article['article_link'])){
                        $operation = '<a href="javascript: void(0);" onclick="article.edit(' . $article_id . ')">修改</a>' .
                            '&nbsp;&nbsp;<a href="' . $article['article_link'] . '" target="_blank">查看</a>';
                    }else{
                        $operation = '<a href="javascript: void(0);" onclick="article.edit(' . $article_id . ')">修改</a>' .
                            '&nbsp;&nbsp;<a href="' . $preview_url . '" target="_blank">预览</a>' .
                            '&nbsp;&nbsp;<a href="' . $publish_url . '" target="_blank">发布</a>';
                    }

                    if ( $article["useFlg"] == 1 ) {
                        $useStatus = '<a href="javascript: void(0);" onclick="article.change.check(' . $article_id . ', ' . $article['useFlg'] .')">启用</a>';
                    } else {
                        $useStatus = '<a href="javascript: void(0);" onclick="article.change.check(' . $article_id . ', ' . $article['useFlg'] .')">禁用</a>';
                    }


                    $result_array['rows'][] = array(
                        'id' => $article_id,
                        'cell' => array(
                            $operation,
                            $article['article_id'],
                            $article['article_title'],
                            $article['article_category_name'],
                            $article['article_summary'],
                            $useStatus
                        )
                    );

                } else {

                    $result_array['rows'][] = array(
                        'id' => $article_id,
                        'cell' => array(
                            $article['article_id'],
                            $article['article_title'],
                            $article['article_category_name'],
                            $article['article_summary'],
                            $useStatus
                        )
                    );

                }


            }
        }

        echo json_encode($result_array);

    }

    /**
     * 保存或修改文章
     *
     */
    private function saveArticle () {

        global $G_PGLOB;

        //实例化文章类
        $contentArticle = new contentArticle();

        $_REQUEST['creator'] = $G_PGLOB->currentUserID;
        $result_array = $contentArticle -> saveArticleInfo($_REQUEST);

        echo json_encode($result_array);

    }

    /**
     * 删除文章信息
     *
     */
    private function deleteArticle() {

        //实例化文章类
        $contentArticle = new contentArticle();

        $result_array = $contentArticle->deleteArticle( $_REQUEST );

        echo json_encode($result_array);

    }

    /**
     * 文章启用/禁用
     */
    private function changeArticleUseflg () {

        //实例化文章类
        $contentArticle = new contentArticle();

        //文章启用/禁用
        $result_array = $contentArticle->changeArticleUseflg( $_REQUEST );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }


}